package leetCode.offer67;

public class Solution2 implements Solution{

    @Override
    public int strToInt(String str) {
        str = str.trim();
        int result = 0;
        StringBuilder num = new StringBuilder();
        //截出数字部分
        for(int i=0;i<str.length();i++) {
            if(i==0){
                if(str.charAt(i)=='-'||str.charAt(i)=='+'){
                    num.append(str.charAt(i));
                    continue;
                }
            }
            if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
                num.append(str.charAt(i));
            }else break;
        }
        // 检查是否只有符号
        if(num.length()==1&&(num.toString().contains("-")||num.toString().contains("+"))){
            return result;
        }
        // 进行数字转换
        else if(num.length()>0) {
            try{
                result = Integer.parseInt(num.toString());
            }catch (Exception e){
                if(num.toString().contains("-")){
                    result = Integer.MIN_VALUE;
                }
                else result = Integer.MAX_VALUE;
            }
        }
        return result;
    }

}
